package com.hk.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.core.roleResult.PageRoleResult;
import com.hk.domain.Activity;
import com.hk.dto.ActivityDto;
import com.hk.dto.AddActivityDto;
import com.hk.mapper.ActivityMapper;
import com.hk.service.ActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@Service
public class ActivityServiceImpl  implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    private final String BEGIN_TIME="beginTime";
    private final String END_TIME="endTime";
    private final String BEGIN_CREATE_TIME="beginCreateTime";
    private final String END_CREATE_TIME="endCreateTime";

    @Override
    public PageRoleResult list(ActivityDto activityDto) {
//        1:条件分页查询活动
        Page<Activity> activityPage = new Page<>(activityDto.getPageNum(),activityDto.getPageSize());
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
//        构造条件 活动编号精确查询 渠道来源精确查询 开始时间 和结束时间
        wrapper.eq(StringUtils.isNotEmpty(activityDto.getCode()),Activity::getCode,activityDto.getCode());
        wrapper.eq(!Objects.isNull(activityDto.getChannel()),Activity::getChannel,activityDto.getChannel());
// 判断创建时间  和活动开始结束时间
        Map<String, Object> params = activityDto.getParams();

//            判断有没有创建时间
            if (!"".equals(params.get(BEGIN_CREATE_TIME))){
            wrapper.between(Activity::getCreateTime, LocalDate.parse(params.get(BEGIN_CREATE_TIME).toString()),LocalDate.parse(params.get(END_CREATE_TIME).toString()));
            }
            if (!"".equals(params.get(BEGIN_TIME))) {
//            活动有开始时间结束  查询也有开始结束时间 查询开始时间或者结束时间在活动时间之内的都可以
                wrapper.between(Activity::getBeginTime, LocalDate.parse(params.get(BEGIN_TIME).toString()), LocalDate.parse(params.get(END_TIME).toString()))
                        .or()
                        .between(Activity::getEndTime, LocalDate.parse(params.get(BEGIN_TIME).toString()), LocalDate.parse(params.get(END_TIME).toString()));
            }

//        分页查询查询数据库返回
        Page<Activity> page= activityMapper.selectPage(activityPage, wrapper);
        PageRoleResult pageRoleResult = new PageRoleResult();
        pageRoleResult.setRows(page.getRecords());
        pageRoleResult.setTotal((int) page.getTotal());
        pageRoleResult.setMsg("查询成功");
        pageRoleResult.setCode(200);
        return pageRoleResult;
    }
//增加活动
    @Override
    public void addActivit(AddActivityDto activityDto) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityDto,activity);
        activity.setCreateTime(LocalDateTime.now());
        activity.setCode(UUID.randomUUID().toString().replace("-", "").substring(0, 8));
        activityMapper.insert(activity);
    }

    @Override
    public Activity selectById(Integer id) {
      return   activityMapper.selectById(id);
    }

    @Override
    public void modifyActivit(AddActivityDto activityDto) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityDto,activity);
        activityMapper.updateById(activity);

    }

    @Override
    public void deleteById(Integer id) {
        activityMapper.deleteById(id);
    }

    @Override
    public List<Activity> listByType(Integer type) {
        LambdaQueryWrapper<Activity> activityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activityLambdaQueryWrapper.eq(Activity::getType,type);
        return activityMapper.selectList(activityLambdaQueryWrapper);

    }
}
